<?php

namespace Addons\OaSystem\Controller;
use Think\ManageBaseController;

class OaDeptController extends OaBaseController{
    var $model;
    var $appid;
    function _initialize() {
        $this->model = $this->getModel ( 'oa_dept' );
        $this->appid = get_app_info()["appid"];
        parent::_initialize ();
        $controller = strtolower(CONTROLLER_NAME);
        $action = strtolower(ACTION_NAME);

        $res ['title'] = '部门信息';
        $res ['url'] = addons_url ( 'OaSystem://OaDept/lists' ,array('mdm'=>I('mdm')));
        $res ['class'] = $action == 'lists' ? 'cur' : '';
        $nav [] = $res;
        $this->assign ( 'nav', $nav );

        $res ['title'] = '部门列表';
        $res ['class'] = $action == 'lists' ? 'cur' : '';
        $sub_nav[] = $res;
        $res ['title'] = '部门等级';
        $res ['url'] = addons_url ( 'OaSystem://OaDeptGrade/lists' ,array('mdm'=>I('mdm')));
        $res ['class'] = $controller == 'oadeptgrade' ? 'cur' : '';
        $sub_nav[] = $res;
        $res ['title'] = '部门关系显示';
        $res ['url'] = addons_url ( 'OaSystem://OaDept/show' ,array('mdm'=>I('mdm')));
        $res ['class'] = $action == 'show' ? 'cur' : '';
        $sub_nav[] = $res;
        $this->assign ( 'sub_nav', $sub_nav );
    }

    function lists(){
        $list_data = $this->_get_model_list ( $this->model );
        $grids = $list_data['list_grids'];
        //$data = $list_data['list_data'];
        $data = db_select('oa_dept', null, 'dept_no asc');
        foreach ($data as $i => $v) {
            $dept_grade = db_find('oa_dept_grade', ['id'=>$v['dept_grade_id']]);
            $parentDept = db_find('oa_dept', ['id'=>$v['pid']]);

            $data[$i]['dept_grade_id'] = $dept_grade['name'];
            $data[$i]['pid'] = $parentDept['name'];

            $editUrl = U('edit', array('id'=> $v['id']));
            $delUrl = U('del', array('id'=> $v['id']));
            //$data[$i]['urls'] = "<a href=$editUrl>编辑</a>".'   '."<a class='confirm' href=$delUrl>删除</a>";
            $data[$i]['urls'] = "<a class='confirm' href=$delUrl>删除</a>";
        }

        $this -> assign('list_grids', $grids);
        $this -> assign('list_data', $data);
        $this->display();
    }

    function add(){
        if (IS_POST) {
            $data = $_POST;
            $data['appid'] = $this->appid;
            if(empty($data['name']) || empty($data['dept_no']) ||
                empty($data['dept_grade_id']) || empty($data['pid'])) {
                $this->error('部门或编号或等级或父级部门没有填!');
                return false;
            }
            $res = db_find('oa_dept', ['dept_no'=>$_POST['dept_no']]);
            if($res != null){
                $this->error ( '创建' . $model ['title'] . '失败！部门编号已存在', U ( 'lists?model=' . $model ['name'], $this->get_param ));
                return false;
            }

            $ret = M('oa_dept')->add($data);
            if ($ret == false) {
                $this->error ( '创建' . $model ['title'] . '失败！', U ( 'lists?model=' . $model ['name'], $this->get_param ));
                return false;
            }

            $dept = db_find('oa_dept', ['name'=>$data['name'], 'dept_no'=>$data['dept_no']]);
            $this->addDeptRoles($dept);
            $this->addDeptDutys($dept);
            $this->addDeptFlows($dept);

            $this->success ( '新增' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ));

        } else {
            $data['grade'] = db_select('oa_dept_grade');
            $map['dept_grade_id'] = array('lt', 3);
            $data['dept'] =  db_select('oa_dept', $map);
            $this->assign ( 'grade', $data['grade'] );
            $this->assign ( 'dept', $data['dept']);
            $this->display();
        }
    }

    function edit(){
        if (IS_POST) {
            $data = $_POST;
            $ret = db_save('oa_dept', ['id'=>$data['id']], $data);
            if ($ret === false) {
                $this->error ( '保存' . $model ['title'] . '失败！', U ( 'lists?model=' . $model ['name'], $this->get_param ));
            }else{
                $this->success ( '保存' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ));
            }
        } else {
            $id = $_GET['id'];
            $data = db_find('oa_dept', ['id'=>$id]);
            $grades = db_select('oa_dept_grade');
            $depts =  db_select('oa_dept');
            foreach($grades as $i => $g) {
                if($data['dept_grade_id'] == $g['id'])
                    $grades[$i]['selected'] = 'selected';
            }
            foreach($depts as $i => $d) {
                if($data['pid'] == $d['id'])
                    $depts[$i]['selected'] = 'selected';
            }
            $this->assign ( 'grade', $grades );
            $this->assign ( 'dept', $depts);
            $this->assign ( 'data', $data);
            $this->display();
        }
    }

    function del()
    {
        $deptid = $_GET['id'];

        if (($dept = db_find('oa_dept', ['id' => $deptid])) == NULL) {
            $this->error('根据ID找不到该部门!');
            return false;
        }
        if(($tmp=db_find('oa_employee', ['dept_id'=>$deptid, 'is_left'=>0])) != NULL) {
            $this->error('该部门还有员工, 请将员工部门修改后再删除部门! - '.$tmp['name']);
            return false;
        }
        if(($tmp=db_find('oa_dept', ['pid'=>$deptid])) != NULL) {
            $this->error('该部门下属部门,请先删除下属部门! - '.$tmp['name']);
            return false;
        }

        if (M('oa_dept')->where(['id'=>$deptid])->delete() == false) {
            $this->error('删除where部门失败!');
            return false;
        }

        if ($dept['dept_grade_id'] == 2) {
            $prefix = $dept['name'];
        } else if ($dept['dept_grade_id'] == 3) {
            $pdept = db_find('oa_dept', ['id' => $dept['pid']]);
            $prefix = $pdept['name'].'_'.$dept['name'];
        } else {
            return false;
        }

        $map['name'] = array('like', $prefix.'%');

        if(($roles = db_select('oa_role', $map)) != NULL) {
            M('oa_role')->where($map)->delete();
            foreach ($roles as $r) {
                M('oa_user_role')->where(['role_id'=>$r['id']])->delete();
            }
        }

        if(($dutys = db_select('oa_duty', $map)) != NULL) {
            $dutyids = array_column($dutys,'id');
            $emp_map['duty_id'] = array('in', $dutyids);
            if(($emps = db_select('oa_employee', $emp_map)) != NULL) {
                myprint('有员工的职务为该部门职务,请重新设置他们的职务!');
                myprint('$emps', $emps);
            }
            M('oa_duty')->where($map)->delete();
            $dutyrole_map['duty_id'] = array('in', $dutyids);
            M('oa_role_duty')->where($dutyrole_map)->delete();
        }

        if(($flows = db_select('oa_process_flow', $map)) != NULL) {
            M('oa_process_flow')->where($map)->delete();
        }

        $this->success ( '删除成功！', U ( 'lists'));
    }

//    function show(){
//        $class1 = db_find('oa_dept_grade', ['grade_no'=>'001']);
//        $class2 = db_find('oa_dept_grade', ['grade_no'=>'002']);
//        $class3 = db_find('oa_dept_grade', ['grade_no'=>'003']);
//        $class1Id = $class1['id'];
//        $class2Id = $class2['id'];
//        $class3Id = $class3['id'];
//        $class1Depts = db_select('oa_dept', ['dept_grade_id'=>$class1Id]);
//        $depts = array();
//        foreach ($class1Depts as $i1 => $v1) {
//            $depts[$i1]['name'] = $v1['name'];
//            $d1Children = db_select('oa_dept', ['dept_grade_id'=>$class2Id, 'pid'=>$v1['id']]);
//            if(!empty($d1Children)) {
//                foreach ($d1Children as $i2 => $v2) {
//                    $depts[$i1]['children'][$i2]['name'] = $v2['name'];
//                    $d2Children = db_select('oa_dept', ['dept_grade_id'=>$class3Id, 'pid'=>$v2['id']]);
//                    if(!empty($d2Children)) {
//                        foreach ($d2Children as $i3 => $v3) {
//                            $depts[$i1]['children'][$i2]['children'][$i3]['name'] = $v3['name'];
//                        }
//                    }
//                }
//            }
//        }
//        myprint($depts);
//        $this->assign('depts', $depts);
//        $this->display();
//    }


    function show(){
        $depts = D("OaDeptView")->order('grade_value')->select();
        $tree = array();
        foreach ($depts as $dept) {
            if(empty($dept['pid'])) {
                $tree[$dept['id']] = array(
                    'name' => $dept['name'],
                    'grade' => intval($dept['dept_grade_id']),
                    'subDept' => array()
                );
            }
            else {
                $this->buildDeptTree($tree, $dept);
            }
        }
        $deptTreeHtmlStr = $this->buildDeptTreeHtml($tree);
        $this->assign('deptTree', $deptTreeHtmlStr);
        $this->display();
    }

    function buildDeptTree(&$tree, $dept) {
        //find father node
        foreach ($tree as $i => $node) {
            if($dept['pid'] == $i) {
                $tree[$i]['subDept'][$dept['id']] = array(
                    'name' => $dept['name'],
                    'grade' => intval($dept['dept_grade_id']),
                    'subDept' => array()
                );
                return true;
            }
        }
        foreach ($tree as $i => $node) {
            if(!empty($node['subDept'])) {
                if($this->buildDeptTree($tree[$i]['subDept'], $dept))
                    return true;
            }
        }
        return false;
    }

    function buildDeptTreeHtml($tree) {
        $treeHtmlStr = '';
        foreach ($tree as $node) {
            if($node['grade'] >= 3 && empty($node['subDept']))  {
                $treeHtmlStr .= '<li><span class="file" style="font-weight:bold;color:#002b36">'.$node['name'].'</span>';
            } else {
                $treeHtmlStr .= '<li><span class="folder" style="font-weight:bold;color:#002b36">'.$node['name'].'</span>';
            }
            $treeHtmlStr .= '<ul>';
            if(!empty($node['subDept'])) {
                $treeHtmlStr .= $this->buildDeptTreeHtml($node['subDept']);
            }
            $treeHtmlStr .= '</ul></li>';
        }
        return $treeHtmlStr;
    }

    private function addDeptDutys($dept) {
        $dutys = array();
        $pdept = db_find('oa_dept', ['id'=>$dept['pid']]);
        if($dept['dept_grade_id'] == 2) {
            $map['level'] = array('in', array(15,16));
            $chu_dutys = db_select('oa_duty', $map, 'duty_no desc');
            $maxDutyNo = intval($chu_dutys[0]['duty_no']);

            $duty = array();
            $duty['appid'] = $this->appId;
            $duty['duty_no'] = sprintf("%06d", $maxDutyNo+1);
            if(strstr($dept['name'], '室') || strstr($dept['name'], '中心')) {
                $duty['name'] = $dept['name'].'_主任';
            } else {
                $duty['name'] = $dept['name'].'_处长';
            }
            $duty['is_del'] = 0;
            $duty['level'] = 16;
            if(db_find('oa_duty', $duty) == NULL) {
                if(M('oa_duty')->add($duty)) {
                    $tmp = db_find('oa_duty', $duty);
                    array_push($dutys, $tmp);
                }
            }
            $duty['duty_no'] = sprintf("%06d", $maxDutyNo+2);
            if(strstr($dept['name'], '室') || strstr($dept['name'], '中心')) {
                $duty['name'] = $dept['name'].'_副主任';
            } else {
                $duty['name'] = $dept['name'].'_副处长';
            }
            $duty['level'] = 15;
            if(db_find('oa_duty', $duty) == NULL) {
                if(M('oa_duty')->add($duty)) {
                    $tmp = db_find('oa_duty', $duty);
                    array_push($dutys, $tmp);
                }
            }
        }

        if($dept['dept_grade_id'] == 3) {
            $map['level'] = array('in', array(11,12));
            $ke_dutys = db_select('oa_duty', $map, 'duty_no desc');
            $maxDutyNo = intval($ke_dutys[0]['duty_no']);

            $duty = array();
            $duty['appid'] = $this->appId;
            $duty['duty_no'] = sprintf("%06d", $maxDutyNo+1);
            $duty['name'] = $pdept['name'].'_'.$dept['name'].'_科长';
            $duty['is_del'] = 0;
            $duty['level'] = 12;
            if(db_find('oa_duty', $duty) == NULL) {
                if(M('oa_duty')->add($duty)) {
                    $tmp = db_find('oa_duty', $duty);
                    array_push($dutys, $tmp);
                }
            }
            $duty['duty_no'] = sprintf("%06d", $maxDutyNo+2);
            $duty['name'] = $pdept['name'].'_'.$dept['name'].'_副科长';
            $duty['level'] = 11;
            if(db_find('oa_duty', $duty) == NULL) {
                if(M('oa_duty')->add($duty)) {
                    $tmp = db_find('oa_duty', $duty);
                    array_push($dutys, $tmp);
                }
            }

            if(strstr($dept['name'], '会') ||
                strstr($dept['name'], '处') ||
                strstr($dept['name'], '室')) {
                $duty['duty_no'] = sprintf("%06d", $maxDutyNo+3);
                if(strstr($dept['name'], '处') || strstr($dept['name'], '室') || strstr($dept['name'], '中心')) {
                    $duty['name'] = $pdept['name'].'_'.$dept['name'].'_主任';
                } else if(strstr($dept['name'], '会')) {
                    $duty['name'] = $pdept['name'].'_'.$dept['name'].'_会长';
                }
                $duty['level'] = 12;
                if(db_find('oa_duty', $duty) == NULL) {
                    if(M('oa_duty')->add($duty)) {
                        $tmp = db_find('oa_duty', $duty);
                        array_push($dutys, $tmp);
                    }
                }

                $duty['duty_no'] = sprintf("%06d", $maxDutyNo+4);
                if(strstr($dept['name'], '处') || strstr($dept['name'], '室') || strstr($dept['name'], '中心')) {
                    $duty['name'] = $pdept['name'].'_'.$dept['name'].'_副主任';
                } else if(strstr($dept['name'], '会')) {
                    $duty['name'] = $pdept['name'].'_'.$dept['name'].'_副会长';
                }
                $duty['level'] = 11;
                if(db_find('oa_duty', $duty) == NULL) {
                    if(M('oa_duty')->add($duty)) {
                        $tmp = db_find('oa_duty', $duty);
                        array_push($dutys, $tmp);
                    }
                }
            }
        }

        //add duty roles
        foreach ($dutys as $duty) {
            $data['appid'] = $this->appid;
            $data['duty_id'] = $duty['id'];

            //加 员工 角色
            if(($role = $this->findRole('员工')) == NULL) {
                $this->error('查找角色ID失败! - '.$rolename);
                return false;
            }
            $data['role_id'] = $role['id'];
            if(db_find('oa_role_duty', $data) == NULL) {
                M('oa_role_duty')->add($data);
            }

            //加 提交申请 角色
            if(($role = $this->findRole('提交申请')) == NULL) {
                $this->error('查找角色ID失败! - '.$rolename);
                return false;
            }
            $data['role_id'] = $role['id'];
            if(db_find('oa_role_duty', $data) == NULL) {
                M('oa_role_duty')->add($data);
            }

            //部门报销审核
            if($duty['level'] == 16) {
                if(($role = $this->findRole($dept['name'].'_部门报销审核')) == NULL) {
                    $this->error('查找角色ID失败! - '.$rolename);
                    return false;
                }
                $data['role_id'] = $role['id'];
                if(db_find('oa_role_duty', $data) == NULL)
                    M('oa_role_duty')->add($data);
            }

            //加班审批
            if($duty['level'] == 11)
                $rolename = $pdept['name'].'_'.$dept['name'].'_副科加班审批';
            if($duty['level'] == 12)
                $rolename = $pdept['name'].'_'.$dept['name'].'_正科加班审批';
            if($duty['level'] == 15)
                $rolename = $dept['name'].'_副处加班审批';
            if($duty['level'] == 16)
                $rolename = $dept['name'].'_正处加班审批';
            if(($role = $this->findRole($rolename)) == NULL) {
                $this->error('查找角色ID失败! - '.$rolename);
                return false;
            }
            $data['role_id'] = $role['id'];
            if(db_find('oa_role_duty', $data) == NULL) {
                M('oa_role_duty')->add($data);
            }

            //休假审批
            if($duty['level'] == 11)
                $rolename = $pdept['name'].'_'.$dept['name'].'_副科休假审批';
            if($duty['level'] == 12)
                $rolename = $pdept['name'].'_'.$dept['name'].'_正科休假审批';
            if($duty['level'] == 15)
                $rolename = $dept['name'].'_副处休假审批';
            if($duty['level'] == 16)
                $rolename = $dept['name'].'_正处休假审批';
            if(($role = $this->findRole($rolename)) == NULL) {
                $this->error('查找角色ID失败! - '.$rolename);
                return false;
            }
            $data['role_id'] = $role['id'];
            if(db_find('oa_role_duty', $data) == NULL) {
                M('oa_role_duty')->add($data);
            }
        }
        
    }

    private function addDeptRoles($dept) {
        $roles = array();
        if($dept['dept_grade_id'] == 2) {
            $roles[0]['name'] = $dept['name'].'_部门报销审核';
            $roles[0]['role_level'] = $dept['dept_grade_id'];
            $roles[0]['role_description'] = '指具有部门初核权限的处长，初核该处的员工报销。';

            $roles[1]['name'] = $dept['name'].'_正处加班审批';
            $roles[1]['role_level'] = 16;
            $roles[1]['role_description'] = '正处级领导审批该处室员工加班申请。';

            $roles[2]['name'] = $dept['name'].'_副处加班审批';
            $roles[2]['role_level'] = 15;
            $roles[2]['role_description'] = '副处级领导审批该处室员工加班申请。';

            $roles[3]['name'] = $dept['name'].'_正处休假审批';
            $roles[3]['role_level'] = 16;
            $roles[3]['role_description'] = '正处级领导审批该处室员工休假申请。';

            $roles[4]['name'] = $dept['name'].'_副处休假审批';
            $roles[4]['role_level'] = 15;
            $roles[4]['role_description'] = '副处级领导审批该处室员工休假申请。';

            $roles[5]['name'] = $dept['name'] .'_副关加班审批';
            $roles[5]['role_level'] = 19;
            $roles[5]['role_description'] = '副关长审批分管处室领导的加班申请。';

            $roles[6]['name'] = $dept['name'] .'_副关休假审批';
            $roles[6]['role_level'] = 19;
            $roles[6]['role_description'] = '副关长审批分管处室领导的休假申请。';
        }

        if($dept['dept_grade_id'] == 3) {
            $pdept = db_find('oa_dept', ['id'=>$dept['pid']]);
            $roles[0]['name'] = $pdept['name'].'_'.$dept['name'].'_正科加班审批';
            $roles[0]['role_level'] = 12;
            $roles[0]['role_description'] = '正科级领导审批该科室员工加班申请。';

            $roles[1]['name'] = $pdept['name'].'_'.$dept['name'].'_副科加班审批';
            $roles[1]['role_level'] = 11;
            $roles[1]['role_description'] = '副科级领导审批该科室员工加班申请。';

            $roles[2]['name'] = $pdept['name'].'_'.$dept['name'].'_正科休假审批';
            $roles[2]['role_level'] = 12;
            $roles[2]['role_description'] = '正科级领导审批该科室员工休假申请。';

            $roles[3]['name'] = $pdept['name'].'_'.$dept['name'].'_副科休假审批';
            $roles[3]['role_level'] = 11;
            $roles[3]['role_description'] = '副科级领导审批该科室员工休假申请。';
        }
        foreach ($roles as $role) {
            $role['appid'] = $this->appId;
            $role['role_scope'] = 'S';
            $role['dept_id'] = $dept['id'];
            if(db_find('oa_role', $role) == NULL) {
                M('oa_role')->add($role);
            }
        }
    }

    private function addDeptFlows($dept) {

        $role_applySubmit = $this->findRole('提交申请');
        $role_guanzhangOtApprove = $this->findRole('加班关长审批');
        $role_guanzhangLvApprove = $this->findRole('休假关长审批');
        $role_hrLeaveApprove = $this->findRole('休假人教审核');
        $role_applyDone = $this->findRole('审批完成');

        //处级加班流程
        if($dept['dept_grade_id'] == 2) {
            $data = array();
            $attr = array(
                'type' => 'OaRole',
                'len' => 3,
                'dept' => intval($dept['id'])
            );

            $rolename1 = $dept['name'].'_副处加班审批';
            $rolename2 = $dept['name'].'_正处加班审批';
            $rolename3 = $dept['name'].'_副关加班审批';
            $role1 = $this->findRole($rolename1);
            $role2 = $this->findRole($rolename2);
            $role3 = $this->findRole($rolename3);
            $operation[0] = $role_applySubmit['id']; //提交
            $operation[1] = intval($role1['id']); //副处
            $operation[2] = intval($role2['id']); //正处
            $operation[3] = intval($role3['id']); //副关
            $operation[4] = $role_guanzhangOtApprove['id'];
            $operation[5] = $role_applyDone['id'];

            $data['appid'] = $this->appId;
            $data['name'] = $dept['name'].'_加班审批流程';
            $data['type'] = 1;
            $data['attr'] = json_encode($attr);
            $data['operations'] = json_encode($operation);

            if(db_find('oa_process_flow', $data) == NULL) {
                M('oa_process_flow')->add($data);
            }
        }

        //科级加班流程
        if($dept['dept_grade_id'] == 3) {
            $data = array();
            $pdept = db_find('oa_dept', ['id'=>$dept['pid']]);
            $attr = array(
                'type' => 'OaRole',
                'len' => 3,
                'dept' => intval($pdept['id']),
                'ke_dept' => intval($dept['id']),
            );
            $rolename1 = $pdept['name'].'_'.$dept['name'].'_副科加班审批';
            $rolename2 = $pdept['name'].'_'.$dept['name'].'_正科加班审批';
            $rolename3 = $pdept['name'].'_副处加班审批';
            $rolename4 = $pdept['name'].'_正处加班审批';
            $rolename5 = $pdept['name'].'_副关加班审批';
            $role1 = $this->findRole($rolename1);
            $role2 = $this->findRole($rolename2);
            $role3 = $this->findRole($rolename3);
            $role4 = $this->findRole($rolename4);
            $role5 = $this->findRole($rolename5);
            $operation[0] = $role_applySubmit['id']; //提交
            $operation[1] = intval($role1['id']); //副科
            $operation[2] = intval($role2['id']); //正科
            $operation[3] = intval($role3['id']); //副处
            $operation[4] = intval($role4['id']); //正处
            $operation[5] = intval($role5['id']); //副关
            $operation[6] = $role_guanzhangOtApprove['id'];
            $operation[7] = $role_applyDone['id'];

            $data['appid'] = $this->appId;
            $data['name'] = $pdept['name'].'_'.$dept['name'].'_加班审批流程';
            $data['type'] = 1;
            $data['attr'] = json_encode($attr);
            $data['operations'] = json_encode($operation);

            if(db_find('oa_process_flow', $data) == NULL) {
                M('oa_process_flow')->add($data);
            }
        }

        //处级休假流程
        if($dept['dept_grade_id'] == 2) {
            $data = array();
            $attr = array(
                'type' => 'OaRole',
                'days' => array('min'=>0, 'max'=>3),
                'dept' => intval($dept['id']),
                'duty_lvl' => array(6,9,11,12,15)
            );

            /////////////////三天内休假审批流程////////////////////
            $rolename = $dept['name'].'_正处休假审批';
            $role = $this->findRole($rolename);
            $operation = array();
            $operation[0] = $role_applySubmit['id']; //提交
            $operation[1] = intval($role['id']); //正处休假审批
            $operation[2] = $role_applyDone['id'];

            $data['appid'] = $this->appId;
            $data['name'] = $dept['name'].'_三天内休假审批流程';
            $data['type'] = 2;
            $data['attr'] = json_encode($attr);
            $data['operations'] = json_encode($operation);

            if(db_find('oa_process_flow', $data) == NULL) {
                M('oa_process_flow')->add($data);
            }

            /////////////////三天内正处级休假审批流程////////////////////
            $rolename = $dept['name'].'_副关休假审批';
            $role = $this->findRole($rolename);
            $operation = array();
            $operation[0] = $role_applySubmit['id']; //提交
            $operation[1] = intval($role['id']); //副关休假审批
            $operation[2] = $role_applyDone['id'];

            $attr['duty_lvl'] = array(16);

            $data['name'] = $dept['name'].'_三天内正处休假审批流程';
            $data['attr'] = json_encode($attr);
            $data['operations'] = json_encode($operation);

            if(db_find('oa_process_flow', $data) == NULL) {
                M('oa_process_flow')->add($data);
            }

            //////////////////三天以上员工休假审批流程///////////////////
            $attr['duty_lvl'] = array(6,9,11,12);
            $attr['days'] = array('min'=>3, 'max'=>-1);

            $rolename = $dept['name'].'_正处休假审批';
            $role = $this->findRole($rolename);
            $operation = array();
            $operation[0] = $role_applySubmit['id']; //提交
            $operation[1] = intval($role['id']); //正处休假审批
            $operation[2] = $role_hrLeaveApprove['id'];
            $operation[3] = $role_applyDone['id'];

            $data['name'] = $dept['name'].'_三天以上员工休假审批流程';
            $data['attr'] = json_encode($attr);
            $data['operations'] = json_encode($operation);

            if(db_find('oa_process_flow', $data) == NULL) {
                M('oa_process_flow')->add($data);
            }

            //////////////////三天以上副处级休假审批流程///////////////////
            $attr['duty_lvl'] = array(15);
            $attr['days'] = array('min'=>3, 'max'=>-1);

            $rolename1 = $dept['name'].'_正处休假审批';
            $rolename2 = $dept['name'].'_副关休假审批';
            $role1 = $this->findRole($rolename1);
            $role2 = $this->findRole($rolename2);
            $operation = array();
            $operation[0] = $role_applySubmit['id']; //提交
            $operation[1] = intval($role1['id']); //正处休假审批
            $operation[2] = intval($role2['id']); //副关休假审批
            $operation[3] = $role_hrLeaveApprove['id'];
            $operation[4] = $role_applyDone['id'];

            $data['name'] = $dept['name'].'_三天以上副处级休假审批流程';
            $data['attr'] = json_encode($attr);
            $data['operations'] = json_encode($operation);

            if(db_find('oa_process_flow', $data) == NULL) {
                M('oa_process_flow')->add($data);
            }

            //////////////////三天以上正处休假审批流程///////////////////
            $attr['duty_lvl'] = array(16);
            $attr['days'] = array('min'=>3, 'max'=>-1);
            $rolename = $dept['name'].'_副关休假审批';
            $role = $this->findRole($rolename);
            $operation = array();
            $operation[0] = $role_applySubmit['id']; //提交
            $operation[1] = intval($role['id']); //副关休假审批
            $operation[2] = $role_guanzhangLvApprove['id'];;
            $operation[3] = $role_hrLeaveApprove['id'];
            $operation[4] = $role_applyDone['id'];


            $data['name'] = $dept['name'].'_三天以上正处休假审批流程';
            $data['attr'] = json_encode($attr);
            $data['operations'] = json_encode($operation);

            if(db_find('oa_process_flow', $data) == NULL) {
                M('oa_process_flow')->add($data);
            }
        }

        //科级休假流程
        if($dept['dept_grade_id'] == 3) {
            $data = array();
            $attr = array(
                'type' => 'OaRole',
                'days' => array('min'=>0, 'max'=>3),
                'dept' => intval($dept['id']),
                'duty_lvl' => array(6,9,11,12)
            );
            $pdept = db_find('oa_dept', ['id'=>$dept['pid']]);

            /////////////////三天内休假审批流程////////////////////
            $rolename = $pdept['name'].'_正处休假审批';
            $role = $this->findRole($rolename);
            $operation = array();
            $operation[0] = $role_applySubmit['id']; //提交
            $operation[1] = intval($role['id']); //正处休假审批
            $operation[2] = $role_applyDone['id'];

            $data['appid'] = $this->appId;
            $data['name'] = $pdept['name'].'_'.$dept['name'].'_三天内休假审批流程';
            $data['type'] = 2;
            $data['attr'] = json_encode($attr);
            $data['operations'] = json_encode($operation);

            if(db_find('oa_process_flow', $data) == NULL) {
                M('oa_process_flow')->add($data);
            }

            //////////////////三天以上科员休假审批流程///////////////////
            $attr['duty_lvl'] = array(6,9);
            $attr['days'] = array('min'=>3, 'max'=>-1);

            $rolename1 = $pdept['name'].'_'.$dept['name'].'_正科休假审批';
            $rolename2 = $pdept['name'].'_正处休假审批';
            $role1 = $this->findRole($rolename1);
            $role2 = $this->findRole($rolename2);
            $operation = array();
            $operation[0] = $role_applySubmit['id']; //提交
            $operation[1] = intval($role1['id']); //正科休假审批
            $operation[2] = intval($role2['id']); //正处休假审批
            $operation[3] = $role_hrLeaveApprove['id'];
            $operation[4] = $role_applyDone['id'];

            $data['name'] = $pdept['name'].'_'.$dept['name'].'_三天以上科员休假审批流程';
            $data['attr'] = json_encode($attr);
            $data['operations'] = json_encode($operation);

            if(db_find('oa_process_flow', $data) == NULL) {
                M('oa_process_flow')->add($data);
            }

            //////////////////三天以上科级休假审批流程///////////////////
            $attr['duty_lvl'] = array(11,12);
            $attr['days'] = array('min'=>3, 'max'=>-1);
            $rolename = $pdept['name'].'_正处休假审批';
            $role = $this->findRole($rolename);
            $operation = array();
            $operation[0] = $role_applySubmit['id']; //提交
            $operation[1] = intval($role['id']); //正处休假审批
            $operation[2] = $role_hrLeaveApprove['id'];
            $operation[3] = $role_applyDone['id'];

            $data['name'] = $pdept['name'].'_'.$dept['name'].'_三天以上科级休假审批流程';
            $data['attr'] = json_encode($attr);
            $data['operations'] = json_encode($operation);

            if(db_find('oa_process_flow', $data) == NULL) {
                M('oa_process_flow')->add($data);
            }
        }
    }

    private function findRole($rolename) {
        $role = db_find('oa_role', ['name'=>$rolename]);
        if($role == NULL) {
            $this->error('找不到角色('.$rolename.')的ID');
            return false;
        }
        return $role;
    }
}
